package config;

import hibernate.IDao;

import java.util.List;

import models.Zone;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.MessageSource;

import service.IDictService;
import utils.StringHelper;

public class SysConfigurationWithoutServletContext implements ApplicationContextAware {
	
	private int pageSize;
	private String rootZone;
	private String loadZone = Constant.FALSE;
	private String loadDict = Constant.FALSE;

	@Autowired
	@Qualifier("commonDAO")
	protected IDao dao;
	
	@Autowired
	private IDictService dictService;
	
	@SuppressWarnings("unchecked")
	public void init() throws Exception{
		
		Constant.PAGE_SIZE = pageSize;
		Constant.ROOT_ZONE_CODE = rootZone;
		
		if(StringHelper.parseBoolean(loadZone)){
			List<Zone> list = dao.findAll("from Zone");
			for(Zone zone : list){
				if(org.apache.commons.lang.StringUtils.isNotEmpty(zone.getCode()))
					Constant.zoneMap.put(zone.getCode(), zone.getName());
			}
			list = null;
		}
		
		if(StringHelper.parseBoolean(loadDict)){
			dictService.refeshDictCache();
		}
	}

	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
		Constant.AppContext = arg0;
		Constant.MESSAGE = (MessageSource)arg0.getBean("messageSource");
	}
	


	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getRootZone() {
		return rootZone;
	}

	public void setRootZone(String rootZone) {
		this.rootZone = rootZone;
	}

	public String getLoadZone() {
		return loadZone;
	}

	public void setLoadZone(String loadZone) {
		this.loadZone = loadZone;
	}

	public String getLoadDict() {
		return loadDict;
	}

	public void setLoadDict(String loadDict) {
		this.loadDict = loadDict;
	}

	
}
